package com.puzzlebazar.client.main.control;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.puzzlebazar.client.main.view.MainView;
import com.puzzlebazar.client.user.UserModel;

public class MessageState  extends MainState {

	private final VerticalPanel mainPanel = new VerticalPanel();
	private final Button okButton = new Button( "Continue" );
	private final ClickHandler okCallback; 
	private final String title;
	private HandlerRegistration clickHandlerRegistration = null;
	
	public MessageState( UserModel model, MainView view, MainController controller, 
			String title, String message ) {
		super( model, view, controller );
	
		this.title = title;
	
		Label messageLabel = new Label( message );
		messageLabel.setWidth( "100%" );

		mainPanel.add( messageLabel );
		mainPanel.add( new HTML( "&nbsp;" ) );
		mainPanel.add( okButton );
		
		mainPanel.setStylePrimaryName( "mainContent" );
		
		okCallback = new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				MessageState.this.controller.forwardToPreviousState();
			}
			
		};
	}
	

	@Override
	public void enter() {
		view.setMainPanelTitle( title );
		
		view.setMainWidget( mainPanel );

		enableCallbacks();
	
	}

	@Override
	public void exit() {
		view.setMainPanelTitle( null );
		disableCallbacks();
	}

	/**
	 * Turns all callbacks on
	 */
	private void enableCallbacks() {
		clickHandlerRegistration = okButton.addClickHandler( okCallback );
	}

	/**
	 * Turns all callbacks on
	 */
	private void disableCallbacks() {
		if( clickHandlerRegistration != null )
			clickHandlerRegistration.removeHandler();
		clickHandlerRegistration = null;
	}


	@Override
	public void release() {
		super.release();
		disableCallbacks();
	}
}
